package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.service.SoundUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.common.vo.SoundUploadResult;
import com.tanhua.dubbo.api.AudioApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.Audio;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.server.vo.AudioVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class TaoHuaService {

    @Autowired
    protected FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @DubboReference(version = "1.0.0")
    private AudioApi audioApi;

    @Autowired
    private AudioMQService audioMQService;

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    @Autowired
    private SoundUploadService soundUploadService;

    /**
     * 发送语音文件
     *
     * @param audioFile
     * @return
     */
    public Object sendSound(MultipartFile audioFile) {
        //上传语音到oss
        SoundUploadResult uploadResult = this.soundUploadService.upload(audioFile);

        if (StrUtil.isEmpty(uploadResult.getName())) {
            return ErrorResult.builder().errCode("500")
                    .errMessage("上传语音失败，请重试！").build();
        }

        //添加到MonDB
        Audio audio = new Audio();
        audio.setUserId(UserThreadLocal.get());     //用户名
        audio.setSoundUrl(uploadResult.getName());   //语音地址
        String audioId = this.audioApi.saveAudio(audio);

        if (StrUtil.isEmpty(audioId)) {
            return ErrorResult.builder().errCode("502")
                    .errMessage("保存语音数据失败，请重试！").build();
        }

        //发送发布成功的消息
        this.audioMQService.audioMsg(audioId);
        return null;
    }

    /**
     * 接收语音
     *
     * @return
     */
    public Object getSound() {
        //获取用户id
        Long userId = UserThreadLocal.get();
        //查询该用户今日发送语音剩余的次数
        Integer count = this.audioApi.queryCount(userId);
        if (count < 1) {
            return ErrorResult.builder().errCode("401")
                    .errMessage("今日桃花传音次数已用完,请明天再来哦~").build();
        }
        //获取一条语音
        Audio audio = this.audioApi.getSound(userId);
        if (ObjectUtil.isEmpty(audio)) {
            return ErrorResult.builder().errCode("500")
                    .errMessage("服务器错误,请稍后再试~").build();
            //给出默认的语音
        }

        //获取发送这条语音的用户信息
        UserInfo userInfo = this.userInfoApi.queryByUserId(audio.getUserId());
        //拷贝属性值
        AudioVo audioVo = BeanUtil.toBeanIgnoreError(userInfo, AudioVo.class);
        BeanUtil.copyProperties(audio, audioVo);
        audioVo.setGender(userInfo.getSex().name().toLowerCase());
        audioVo.setRemainingTimes(count-1);
        return audioVo;
    }
}
